﻿//======================================================================
//
//        Copyright (C) 2008 HOHO``
//        All rights reserved
//
//        filename :$safeitemrootname$
//        description :
//
//        created by QiuZhongJie at  $time$
//======================================================================


using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;
using System.Collections.Generic;

namespace Hisun.Controllers
{
    public class AbcLogin
    {
        private HisunDBDataContext db = new HisunDBDataContext();
        private string lastError;
        private TSCRB_JS_Users currectUser;

        public bool isAccountLock
        {
          
                          
            get {
                var myLock = (from a in db.TAbc_JS_Users
                              where a.操作员ID == currectUser.操作员ID
                              select a).Single();


                return myLock.停用;
            }
        }

        public string LastError
        {
            get { return lastError; }
            private set { lastError = value; }
        }


        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="username">操作员ID</param>
        public AbcLogin(string username)
        {
            if (db.TAbc_JS_Users.Count(u => u.操作员ID == username) == 0)
            {
                throw new Exception("没有该操作员ID");
            }
            currectUser = (from u in db.TSCRB_JS_Users
                           where u.操作员ID == username
                           select u).Single();
        }

        /// <summary>
        /// 检查密码
        /// </summary>
        /// <param name="password">密码</param>
        /// <param name="isHash">是否已经加密</param>
        /// <returns></returns>
        public int CheckPassword(string password, bool isHash)
        {
            if (!isHash)
                password = AbcAccount.EncryptPassword(password);
            var mypassword = (from a in db.TAbc_JS_Users
                              where a.操作员ID == currectUser.操作员ID
                              select a).Single();
            if (password == mypassword.密码)
                return 0;
            else
            {
                lastError = "密码错误!";
                return -1;
            }
        }

        /// <summary>
        /// 检查密码
        /// </summary>
        /// <param name="ppassword">密码</param>
        /// <returns></returns>
        internal int CheckPassword(string password)
        {
            return CheckPassword(password, false);
        }

        /// <summary>
        /// 获得所有权限ID
        /// </summary>
        /// <returns></returns>
        public List<int> GetOwnAuthIDs()
        {
            AbcSecurity security = new AbcSecurity();
            var auths = currectUser.权限ID.Split(',');
            var roles = currectUser.角色ID.Split(',');
            List<int> retval = new List<int>();
            foreach (var s in auths)
            {
                if (!string.IsNullOrEmpty(s))
                    retval.Add(Convert.ToInt32(s));
            }
            foreach (var s in roles)
            {
                var ret = security.GetAuthsOfRole(Convert.ToInt32(s));
                if (ret != null)
                    retval.AddRange(ret);
            }
            return retval;
        }



        /// <summary>
        /// 登录成功，赋值浏览器Session
        /// </summary>
        /// <returns></returns>
        public int Enter()
        {
            HttpContext.Current.Session[ServerVars.sysSessionUserName] = currectUser.操作员ID;
            HttpContext.Current.Session[ServerVars.sysSessionUserAuths] = GetOwnAuthIDs();
            HttpContext.Current.Session[ServerVars.sysSessionSkinID] = "MSN_Blue";
            return 0;
        }
    }
}
